package library.view;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import library.dao.Impl.UserDaoImpl;
import library.pojo.User;

public class AddUserDialog extends JDialog {
	private JTextField userNameTextField;
	private JTextField passwordTextField;
	private JTextField confirmPasswordTextField;
	private JTextField nameTextField;
	private JTextField phoneTextField;
	private JComboBox searchTypeComboBox;
	private ImageIcon icon;

	public static void main(String[] args) {
		new AddUserDialog();
	}

	public AddUserDialog() {
		icon = new ImageIcon(MainJTPane.class.getResource("/title.png"));
		this.setIconImage(icon.getImage());
		this.setTitle("添加用户");
		setBounds(100, 100, 440, 620);
		this.setLayout(null);

		JLabel registerLabel = new JLabel("添加用户");
		registerLabel.setIcon(new ImageIcon("res/add.png"));
		registerLabel.setFont(new Font("宋体", Font.PLAIN, 40));
		registerLabel.setBounds(110, 40, 226, 59);
		this.add(registerLabel);

		String[] type = { "借阅者", "图书管理员", "系统管理员" };
		searchTypeComboBox = new JComboBox(type);
		searchTypeComboBox.setFont(new Font("宋体", Font.PLAIN, 14));
		searchTypeComboBox.setToolTipText("");
		searchTypeComboBox.setBounds(151, 129, 215, 22);
		this.add(searchTypeComboBox);

		JLabel userTypeLabel = new JLabel("用户类型：");
		userTypeLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		userTypeLabel.setBounds(25, 126, 112, 24);
		this.add(userTypeLabel);

		JLabel userNameLabel = new JLabel("  用户名：");
		userNameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		userNameLabel.setBounds(25, 186, 112, 24);
		this.add(userNameLabel);

		JLabel passwordLabel = new JLabel("    密码：");
		passwordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		passwordLabel.setBounds(25, 246, 112, 24);
		this.add(passwordLabel);

		JLabel confirmPasswordLabel = new JLabel("确认密码：");
		confirmPasswordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		confirmPasswordLabel.setBounds(25, 306, 112, 24);
		this.add(confirmPasswordLabel);

		JLabel nameLabel = new JLabel("    姓名：");
		nameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		nameLabel.setBounds(25, 366, 112, 24);
		this.add(nameLabel);

		JLabel phoneLabel = new JLabel("联系电话：");
		phoneLabel.setFont(new Font("宋体", Font.PLAIN, 20));
		phoneLabel.setBounds(25, 426, 112, 24);
		this.add(phoneLabel);

		userNameTextField = new JTextField();
		userNameTextField.setBounds(151, 189, 215, 22);
		this.add(userNameTextField);
		userNameTextField.setColumns(10);

		passwordTextField = new JTextField();
		passwordTextField.setColumns(10);
		passwordTextField.setBounds(151, 249, 215, 22);
		this.add(passwordTextField);

		confirmPasswordTextField = new JTextField();
		confirmPasswordTextField.setColumns(10);
		confirmPasswordTextField.setBounds(151, 309, 215, 22);
		this.add(confirmPasswordTextField);

		nameTextField = new JTextField();
		nameTextField.setColumns(10);
		nameTextField.setBounds(151, 369, 215, 22);
		this.add(nameTextField);

		phoneTextField = new JTextField();
		phoneTextField.setColumns(10);
		phoneTextField.setBounds(151, 429, 215, 22);
		this.add(phoneTextField);

		JButton registerButton = new JButton("添加");
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				registerActionPerformed(e);
			}
		});
		registerButton.setBounds(138, 488, 167, 39);
		this.add(registerButton);

		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	private void registerActionPerformed(ActionEvent e) {
		UserDaoImpl userDao = new UserDaoImpl();
		String userName = this.userNameTextField.getText();
		String password = new String(this.passwordTextField.getText());
		String confirmPassword = this.confirmPasswordTextField.getText();
		String phone = this.phoneTextField.getText();
		String name = this.nameTextField.getText();
		if (this.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "用户名不能为空！");
			return;
		}
		if (userDao.register(userName) != null) {
			JOptionPane.showMessageDialog(null, "该用户已存在！");
			return;
		}
		if (this.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空！");
			return;
		}
		if (password.length() < 3) {
			JOptionPane.showMessageDialog(null, "密码长度不得小于3位！");
			return;
		}
		if (this.isEmpty(confirmPassword)) {
			JOptionPane.showMessageDialog(null, "请确保两次输入的密码一致！");
			return;
		}
		if (this.isNotSame(password, confirmPassword)) {
			JOptionPane.showMessageDialog(null, "请确保两次输入的密码一致！");
			return;
		}
		if (this.isEmpty(name)) {
			JOptionPane.showMessageDialog(null, "姓名不能为空！");
			return;
		}
		if (this.isEmpty(phone)) {
			JOptionPane.showMessageDialog(null, "联系电话不能为空！");
			return;
		}
		if (!phone.matches("[0-9]{0,11}") || phone.length() != 11) {
			JOptionPane.showMessageDialog(null, "请输入正确的手机号！");
			return;
		}

		String type = (String) searchTypeComboBox.getSelectedItem();
		User user = new User(userName, password, name, type, phone);
		userDao.addUser(user);
		JOptionPane.showMessageDialog(null, "添加成功！");
		this.dispose();
	}

	/**
	 * 判断是否为空
	 */
	private boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断两次输入的密码是否一致
	 */
	private boolean isNotSame(String str1, String str2) {
		if (str1.equals(str2)) {
			return false;
		} else {
			return true;
		}
	}
}
